#include "ResourceManager.h"
namespace Fanfou
{
	ResourceManager::ResourceManager():
		movieNormal(":/LoadingAni","GIF"),
		movieNormalUseCount(0),
		movieCircle(":/CircleLoadingAni","GIF"),
		movieCircleUseCount(0),
		movieToolbar(":/BtnLoadingAni","GIF"),
		movieToolbarUseCount(0){}

	QMovie* ResourceManager::getMovie(ResourceType type)
	{
		switch(type)
		{
			case NormalMovie:
				++movieNormalUseCount;
				if(movieNormalUseCount == 1)
					movieNormal.start();
				return &movieNormal;
			case CircleMovie:
				++movieCircleUseCount;
				if(movieCircleUseCount == 1)
					movieCircle.start();
				return &movieCircle;
			case ToolbarMovie:
				++movieToolbarUseCount;
				if(movieToolbarUseCount == 1)
					movieToolbar.start();
				return &movieToolbar;
			default:
				return 0;
		}
	}

	void ResourceManager::returnMovieResource(ResourceType type)
	{
		switch(type)
		{
			case NormalMovie:
				if(movieNormalUseCount == 0)
					return;
				else if(movieNormalUseCount == 1)
					movieNormal.stop();
				--movieNormalUseCount;
				break;
			case CircleMovie:
				if(movieCircleUseCount == 0)
					return;
				else if(movieCircleUseCount == 1)
					movieCircle.stop();
				--movieCircleUseCount;
				break;
			case ToolbarMovie:
				if(movieToolbarUseCount == 0)
					return;
				else if(movieToolbarUseCount == 1)
					movieToolbar.stop();
				--movieToolbarUseCount;
				break;
			default:
				break;
		}
	}

	QLabel* ResourceManager::getLabel(ResourceType type)
	{
		switch(type)
		{
			case RelatedImageLabel:
				if(relatedImageLabels.size() > 0)
					return relatedImageLabels.takeLast();
				else
				{
					QLabel* label = new QLabel();
					label->setObjectName("RelatedImageLabel");
					label->setCursor(Qt::PointingHandCursor);
					return label;
				}
			case ImageLoadingLabel:
				if(imageLoadingLabels.size() > 0)
					return imageLoadingLabels.takeLast();
				else
				{
					QLabel* label = new QLabel();
					label->setObjectName("ImageProgressLabel");
					label->setAlignment(Qt::AlignCenter);
					return label;
				}
			default:
				return 0;
		}
	}

	void ResourceManager::returnLabelResource(QLabel* l, ResourceType type)
	{
		l->clear();
		l->hide();
		l->setParent(0);
		switch(type)
		{
			case RelatedImageLabel:
				relatedImageLabels.append(l);
				break;
			case ImageLoadingLabel:
				imageLoadingLabels.append(l);
				break;
			default:
				break;
		}
	}
}
